package com.xcm.config;

import com.xcm.constant.SysAuthorityConstants;
import com.xcm.dao.SysAuthorityMapper;
import com.xcm.model.SysAuthority;
import com.xcm.util.SpringContextUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import java.util.*;

/**
 * 自定义初始化加载类（可从数据库加载数据）
 * created by lq at 2018-04-15 15:07
 *
 * @author linqiang
 */
@Component
public class MyConfig implements InitializingBean, ServletContextAware {

    /**
     * 系统（key为系统标志）
     */
    public static Map<String, SysAuthority> systemMap = new HashMap<>();

    @Autowired
    private SysAuthorityMapper sysAuthorityMapper;

    /**
     * 名族集合
     */
    public static List<String> nationList;

    static {
        nationList = Arrays.asList(
                "汉族", "蒙古族", "回族", "藏族", "维吾尔族",
                "苗族", "彝族", "壮族", "布依族", "朝鲜族",
                "满族", "侗族", "瑶族", "白族", "土家族",
                "哈尼族", "哈萨克族", "傣族", "黎族", "僳僳族",
                "佤族", "畲族", "高山族", "拉祜族", "水族",
                "东乡族", "纳西族", "景颇族", "柯尔克孜族", "土族",
                "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族",
                "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族",
                "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族",
                "德昂族", "保安族", "裕固族", "京族", "塔塔尔族",
                "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族");
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        //查询所有系统
        Map<String, Object> params = new HashMap<>();
        params.put("type", SysAuthorityConstants.SYS_AUTHORITY_TYPE_SYSTEM);
        List<SysAuthority> systemList = sysAuthorityMapper.list(params);
        if (CollectionUtils.isNotEmpty(systemList)) {
            for (int i = 0; i < systemList.size(); i++) {
                //系统对象
                if (StringUtils.isNotBlank(systemList.get(i).getSign())) {
                    systemMap.put(systemList.get(i).getSign(), systemList.get(i));
                }
            }
        }
    }

    /**
     * 文件上传配置
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
//        //文件最大
//        factory.setMaxFileSize("2048KB"); //2MB
//        /// 设置总上传数据总大小
//        factory.setMaxRequestSize("10240KB"); //10MB
        return factory.createMultipartConfig();
    }

    /**
     * spring上下文工具类
     *
     * @return
     */
    @Bean
    public SpringContextUtil setSpringContextUtil() {
        return new SpringContextUtil();
    }
}
